776F - Sherlock's bet to Moriarty - CodeForces Solution


constructive algorithms data structures divide and conquer geometry graphs implementation trees *2800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

typedef long double ld;
typedef long long ll;
using namespace std;
int di[] = {1, 0, -1, 0};
int dj[] = {0, 1, 0, -1};
const ll oo = 1e18, MOD = 1e9 + 7;
const int N = 1e5 + 5, M = 1e6 + 5;
const ld PI = acos(-1.0), EPS = 1e-9;

int t, n, m, k, a[N], b[N], sz[N], vis[N];
int ans[N];
vector<int> v[N];
ll tot;

int find_sizes(int node, int p) {
    sz[node] = 1;
    for (auto x:v[node]) if (x != p && !vis[x]) sz[node] += find_sizes(x, node);
    return sz[node];
}

int get_centroid(int node, int p, int size) {
    for (auto x:v[node]) {
        if (x == p || vis[x]) continue;
        if (sz[x] > size) return get_centroid(x, node, size);
    }
    return node;
}

void init_centroid(int node, int level) {
    find_sizes(node, 0);
    int c = get_centroid(node, -1, sz[node] / 2);
    ans[c] = level;
    vis[c] = 1;
    for (auto x:v[c]) if (!vis[x]) init_centroid(x, level + 1);
}


//#define endl '\n'
int main() {
    //ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen("marsllino.in", "r", stdin);
    //memset(dp, -1, sizeof dp);
    cin >> n >> k;
    set<int> s;
    for (int i = 1; i <= n; i++) s.insert(i);
    vector<pair<int, int>> tmp;
    for (int i = 1; i <= k; i++) {
        cin >> a[i] >> b[i];
        if (a[i] > b[i]) swap(a[i], b[i]);
        tmp.push_back({min(b[i] - a[i] - 1, n - (b[i] - a[i]) - 1), i});
    }
    sort(tmp.begin(), tmp.end());
    vector<vector<int>> v2;
    for (auto x:tmp) {
        vector<int> me = {a[x.second], b[x.second]};
        if (b[x.second] - a[x.second] - 1 < n - (b[x.second] - a[x.second]) - 1) {
            auto it = s.upper_bound(a[x.second]);
            while (it != s.end() && *it < b[x.second]) {
                me.push_back(*it);
                s.erase(it);
                it = s.upper_bound(a[x.second]);
            }
        } else {
            while (!s.empty() && *s.begin() < a[x.second]) {
                me.push_back(*s.begin());
                s.erase(s.begin());
            }
            while (!s.empty() && *s.rbegin() > b[x.second]) {
                me.push_back(*s.rbegin());
                s.erase(*s.rbegin());
            }
        }
        sort(me.rbegin(), me.rend());
        v2.push_back(me);
    }
    vector<int> remain;
    while (!s.empty()) {
        remain.push_back(*s.rbegin());
        s.erase(*s.rbegin());
    }
    v2.push_back(remain);
    sort(v2.begin(), v2.end());
    map<pair<int, int>, int> mp;
    int it = 0;
    for (auto x:v2) {
        it++;
        for (int i = 0; i < x.size(); i++) {
            int node1 = x[i], node2 = x[(i + 1) % x.size()];
            if (!mp[{node1, node2}]) mp[{node1, node2}] = mp[{node2, node1}] = it;
            else {
                v[mp[{node1, node2}]].push_back(it);
                v[it].push_back(mp[{node1, node2}]);
            }
        }
    }
    init_centroid(1, 1);
    for (int i = 1; i <= k + 1; i++) cout << ans[i] << " ";
    return 0;
}


Comments

Submit
0 Comments
More Questions

1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization